home *** CD-ROM | disk | FTP | other *** search
- /* ***** BEGIN LICENSE BLOCK *****
- * Version: MPL 1.1/GPL 2.0/LGPL 2.1
- *
- * The contents of this file are subject to the Mozilla Public License Version
- * 1.1 (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS" basis,
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- * for the specific language governing rights and limitations under the
- * License.
- *
- * The Original Code is Fasterfox.
- *
- * The Initial Developer of the Original Code is
- * Tony Gentilcore.
- * Portions created by the Initial Developer are Copyright (C) 2005
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s):
- * See readme.txt
- *
- * Alternatively, the contents of this file may be used under the terms of
- * either the GNU General Public License Version 2 or later (the "GPL"), or
- * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
- * in which case the provisions of the GPL or the LGPL are applicable instead
- * of those above. If you wish to allow use of your version of this file only
- * under the terms of either the GPL or the LGPL, and not to allow others to
- * use your version of this file under the terms of the MPL, indicate your
- * decision by deleting the provisions above and replace them with the notice
- * and other provisions required by the GPL or the LGPL. If you do not delete
- * the provisions above, a recipient may use your version of this file under
- * the terms of any one of the MPL, the GPL or the LGPL.
- *
- * ***** END LICENSE BLOCK ***** */
-
- // local references to XPCOM services
- var PrefetchService = Components.classes["@mozilla.org/prefetch-service;1"].getService(Components.interfaces.nsIPrefetchService);
- var PreferencesService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
-
- // Timer global variables
- var StartTime;
- var StopTime;
- var Finished;
-
- // Robots.txt cache
- var robotsCacheMax = 64;
- var robotsCacheTop = 0;
- var robotsCache = new Array(robotsCacheMax);
- var optOut = new Array(robotsCacheMax);
-
- // Setup the extension core
- function FF_init() {
-
- // because of the nature of overlays and the panel selection code,
- // this is reproduced from chrome://browser/content/pref/pref.xul, and
- // refocuses the prefsCategories window in order to ensure a clean and
- // correct selection of the right panel
- var prefsCategories = document.getElementById("prefsCategories");
- if (prefsCategories) {
- var lastPanel = 0, button;
- try {
- lastPanel = PreferencesService.getIntPref("browser.preferences.lastpanel");
- }
- catch (e) {}
-
- prefsCategories.focus();
- button = prefsCategories.childNodes[lastPanel];
- if (button) {
- document.getElementById("panelFrame").setAttribute("src", button.getAttribute("url"));
- button.checked = true;
- }
- }
-
- // Register the web progress listener for the page load timer
- gBrowser.addProgressListener(ffListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
-
- // show or hide the timer
- if(PreferencesService.getBoolPref("extensions.fasterfox.pageLoadTimer", true)) {
- document.getElementById("fasterfox-statusbar").setAttribute("hidden", "false");
- } else {
- document.getElementById("fasterfox-statusbar").setAttribute("hidden", "true");
- }
-
- return true;
- }
-
- // Tear down the extension
- function FF_uninit() {
- gBrowser.removeProgressListener(ffListener);
- }
-
- // GetAbsoluteURL
- function getAbsoluteUrl(url, docUrl) {
- if(url && url.indexOf('://')>0) return url;
- docUrl=(docUrl)? docUrl.substring(0,docUrl.lastIndexOf('/')+1):dynapi.documentPath;
- url=url.replace(/^(.\/)*/,'');
- docUrl=docUrl.replace(/(\?.*)$/,'').replace(/(#.*)*$/,'').replace(/[^\/]*$/,'');
- if (url.indexOf('/')==0) return docUrl.substring(0,docUrl.indexOf('/',docUrl.indexOf('//')+2))+url;
- else while(url.indexOf('../')==0){
- url=url.replace(/^..\//,'');
- docUrl=docUrl.replace(/([^\/]+[\/][^\/]*)$/,'');
- };
- return docUrl+url;
- }
-
- // Gets the base url
- function getBaseURL(url) {
- var startPos = url.indexOf("://")+3;
- var endPos = url.indexOf('/', startPos);
- if (endPos < 0) endPos = url.length;
- var baseUrl = url.substring(0, endPos);
- return baseUrl;
- }
-
- // Checks a given site's robots.txt file to see if they wish to opt-out of prefetching
- function isSiteOptOut(url, aEvent, pos) {
- var fileName = getBaseURL(url) + "/robots.txt";
-
- // load from cache if possible
- var i;
- for(i = 0; i < robotsCacheMax; i++) {
- if(fileName == robotsCache[i]) {
- return optOut[i];
- }
- }
-
- // it is not cached, so load it
- var req = new XMLHttpRequest();
- req.open("GET", fileName, true);
- req.onreadystatechange = function () {
- if (req.readyState == 4) {
- if (req.status == 200) {
- var result = (req.responseText.toLowerCase().indexOf("fasterfox")>=0);
- robotsCacheTop = (robotsCacheTop+1) % robotsCacheMax;
- robotsCache[robotsCacheTop] = fileName;
- optOut[robotsCacheTop] = result;
- //doEnhancedPrefetch(aEvent, pos);
- }
- }
- };
- req.send(null);
-
- return -1;
- }
-
- // Converts a string URL into a URI object
- function makeURI(aURL) {
- var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
- return ioService.newURI(aURL, null, null);
- }
-
- // Iterates through all the <a> links on a page and adds
- // them to the queue to be prefetched.
- function doEnhancedPrefetch(aEvent, pos) {
-
- // exit if enhanced prefetching is turned off
- // or if the site has opted out of prefetching
- if(!PreferencesService.getBoolPref("extensions.fasterfox.enhancedPrefetching")) return;
-
- // doc is document that triggered "onload" event
- var doc = aEvent.originalTarget;
-
- // if this isn't an http:// page, retun
- if (doc.location.href.indexOf("http://") != 0) return;
-
- // load whitelist
- var whitelist = PreferencesService.getCharPref("extensions.fasterfox.whitelist").split(",");
- var whitelistLen = whitelist.length;
-
- // find all links
- var a = doc.getElementsByTagName('a');
- var href, numLinksToPrefetch, i, j;
-
- // prefetch a max of 100 links
- numLinksToPrefetch = (a.length>100) ? 100 : a.length;
-
- // if its the first time, set the pos to 0,
- // if a pos was passed in, we'll start the for loop there instead
- if (pos == null || pos <=0) pos = 0;
-
- // iterate through the links
- mainLinkLoop:
- for (i=pos; i<numLinksToPrefetch; i++) {
- href = a[i].getAttribute('href').toLowerCase();
-
- // make sure there is a link
- if(href && href.length > 4) {
-
- // Don't prefetch links w/ query strings
- // or the same page we are on
- if(href.indexOf('?')>=0 || href == doc.location.href) continue;
-
- // Don't prefetch things that look like a logout link
- // Special thanks to b1naryb0y and Kai Risku for this bug fix
- if(href.indexOf('logout')>=0 || href.indexOf('logoff')>=0) continue;
-
- // Only prefetch static content, this causes dynamic pages to be ignored
- fileExtensionStartPos = href.length-4;
- if(!(href.indexOf('.htm') == fileExtensionStartPos
- || href.indexOf('.html') == fileExtensionStartPos-1
- || href.indexOf('.jpg') == fileExtensionStartPos
- || href.indexOf('.gif') == fileExtensionStartPos
- || href.indexOf('.png') == fileExtensionStartPos
- || href.indexOf('.jpeg') == fileExtensionStartPos-1
- || href.indexOf('.txt') == fileExtensionStartPos
- || href.indexOf('.text') == fileExtensionStartPos-1
- || href.indexOf('.xml') == fileExtensionStartPos
- || href.indexOf('.pdf') == fileExtensionStartPos)) continue;
-
- // Don't prefetch whitelist links
- href = getAbsoluteUrl(href, doc.location.href);
- for(j=0; j<whitelistLen; j++) {
- if(whitelist[j].length > 0 && href.indexOf(whitelist[j]) >= 0) continue mainLinkLoop;
- }
-
- // test if site is opting out of prefetching
- var shouldOptOut = isSiteOptOut(href, aEvent, i);
- if(shouldOptOut) {
- continue;
- } else if (shouldOptOut == -1) {
- return;
- }
-
- try {
- PrefetchService.prefetchURI(makeURI(getAbsoluteUrl(a[i].getAttribute('href'), doc.location.href)), makeURI(doc.location.href), true);
- } catch(e) {
- //if(e.name != "NS_ERROR_ABORT") alert(getAbsoluteUrl(href, doc.location.href) + '\n' + e.toString());
- }
- }
- }
-
- }
-
- // Converts a Delta date into an english string
- // showMilliseconds = true, gives fractions of seconds
- // showMilliseconds = false, gives whole seconds
- function TimeStr(delta, showMilliseconds) {
-
- // Calc seconds and milliseconds
- var mseconds = delta % 1000;
- delta = (delta - mseconds) / 1000;
- var seconds = delta.toString();
-
- if(showMilliseconds) {
- mseconds = mseconds.toString();
- var pd = ''; var i;
- if (3 > mseconds.length) {
- for (i = 0; i < (3-mseconds.length); i++) {
- pd += '0';
- }
- }
- mseconds = mseconds + pd;
- } else {
- mseconds = "000";
- }
-
- return(seconds + "." + mseconds + "s");
- }
-
- // Updates the display of the page load timer
- function ff_updateTimer() {
- var Delta;
- if (!Finished) {
- CurrentTime = new Date();
- Delta = CurrentTime.getTime() - StartTime.getTime();
- document.getElementById("fasterfox-label").value = TimeStr(Delta, false);
- setTimeout('ff_updateTimer()',1000);
- } else {
- Delta = StopTime.getTime() - StartTime.getTime();
- document.getElementById("fasterfox-label").value = TimeStr(Delta, true);
- }
- }
-
- // Ends page load timer
- function ff_stopTimer() {
- // Stop timer
- StopTime = new Date();
- Finished = true;
- ff_updateTimer();
- }
-
- // Starts page load timer
- function ff_startTimer() {
- // Start timer
- StartTime = new Date();
- Finished = false;
- ff_updateTimer();
- }
-
- // Show or hide the page load timer
- // if show = true, it will be displayed
- // if show = false, it will be hidden
- function ff_showTimer(show) {
- PreferencesService.setBoolPref("extensions.fasterfox.pageLoadTimer", show);
- var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
- .getService(Components.interfaces.nsIWindowMediator);
- var win = wm.getMostRecentWindow("navigator:browser");
- if (win) {
- win.document.getElementById("fasterfox-statusbar").setAttribute("collapsed", !show);
- win.document.getElementById("fasterfox-label").value = "Fasterfox";
- }
- }
-
- // Clears the browser disk and memory cache
- function ff_clearCache() {
- var cacheService = Components.classes["@mozilla.org/network/cache-service;1"]
- .getService(Components.interfaces.nsICacheService);
- cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
- cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
- }
-
- // Shows the Fasterfox options pane
- function ff_openOptions() {
- var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
- .getService(Components.interfaces.nsIWindowMediator);
- var win = wm.getMostRecentWindow("Fasterfox:Options");
- if (win) {
- win.focus();
- } else {
- openDialog("chrome://fasterfox/content/pref-fasterfox.xul", "", "chrome,titlebar,toolbar,centerscreen,modal");
- }
- }